package com.aispeech.task.api.data.form;

import com.aispeech.enums.EAudioType;
import com.aispeech.enums.ETaskType;
import com.aispeech.task.api.data.enums.Errors;
import com.aispeech.data.pojo.task.Request;
import com.aispeech.task.api.exceptions.ParamsError;
import com.aispeech.utils.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class NewTaskForm {
    public static Request fromMap(Map<String, String> map) {
        ParamsError error = null;

        String[] hotwords = null;
        String paramHotwords = map.get("hotwords");
        if (!StringUtils.isNullOrEmpty(paramHotwords)) {
            hotwords = paramHotwords.split(",");
        }

        Request request = Request.builder()
                .productId(map.getOrDefault("productId", map.getOrDefault("app_id", "")))
                .audioType(EAudioType.fromKey(map.get("audio_type")))
                .lmid(map.getOrDefault("lmid", ""))
                .phraseFile(map.getOrDefault("phrase_file", ""))
                .hotwords(hotwords)
                .audioLength(Long.valueOf(map.getOrDefault("file_len", "0")))
                .audioFilePath(map.getOrDefault("file_path", ""))
                .duration(Long.valueOf(map.getOrDefault("duration", "0")))
                .speakerNumber(Integer.valueOf(map.getOrDefault("speaker_number", "0")))
                .channel(Integer.valueOf(map.getOrDefault("channel", "1")))
                .sampleBytes(Integer.valueOf(map.getOrDefault("sample_bytes", "2")))
                .taskType(ETaskType.fromKey(map.getOrDefault("task_type", "1")))
                .useInverseTxt(Integer.valueOf(map.getOrDefault("use_inverse_txt", "1")))
                .useTxtSmooth(Integer.valueOf(map.getOrDefault("use_txt_smooth", "1")))
                .inversedTxtSmooth(Integer.valueOf(map.getOrDefault("inverse_txt_smooth", "0")))
                .sensitiveWordsLexFile(map.getOrDefault("sensitive_file", ""))
                .callback(map.getOrDefault("callback", ""))
                .debug(Integer.valueOf(map.getOrDefault("debug", "0")))
                .build();

        if (StringUtils.isNullOrEmpty(request.getProductId())) {
            error = new ParamsError(Errors.ERR_CHECK_PARAM, "missing param: app_id");
        } else if ((request.getAudioType() == null)) {
            error = new ParamsError(Errors.ERR_CHECK_PARAM, "unkown param: audio_type");
        } else if (StringUtils.isNullOrEmpty(request.getAudioFilePath())) {
            error = new ParamsError(Errors.ERR_CHECK_PARAM, "missing param: file_path");
        }

        if (error != null) throw error;

        if (StringUtils.isNullOrEmpty(request.getAudioFilePath())) {
            error = new ParamsError(Errors.ERR_CHECK_PARAM, "missing param: file_path");
        } else {
            try {
                new URL(request.getAudioFilePath());
            } catch (MalformedURLException e) {
                error = new ParamsError(Errors.ERR_CHECK_PARAM, "unkown param: file_path");
            }
        }

        if (!StringUtils.isNullOrEmpty(request.getCallback())) {
            try {
                new URL(request.getCallback());
            } catch (MalformedURLException e) {
                error = new ParamsError(Errors.ERR_CHECK_PARAM, "unkown param: callback");
            }
        }

        if (error != null) {
            throw error;
        } else {
            return request;
        }
    }
}
